package general;

import conexion.CConexion;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import validaciones.CLogin;

public class CPt100 extends CConexion {

    private int snoas = -1;
    private int requisicion = -1;
    private String compania = null;
    private CLogin loginU = new CLogin();

    public CPt100() {
        super();
        this.compania = loginU.getCompaniaActual();
    }

    public boolean setSnoas() throws SQLException {
        ResultSet rsBuscarPtNume = null;
        Statement stncBuscarPtNume = this.getConexion().createStatement();
        String sqlBuscarPtNume = "SELECT pt100nume FROM pt100 WHERE pt100tire='NS' and pt100copr='CON' and pt100cose='NOAS' and pt100nocia='" + loginU.getCompaniaActual() + "'";
        rsBuscarPtNume = stncBuscarPtNume.executeQuery(sqlBuscarPtNume);
        if (rsBuscarPtNume.next()) {
            snoas = rsBuscarPtNume.getInt(1);
            return true;
        } else {
            return false;
        }
    }

    public void actualizaNoas() throws SQLException {
        snoas += 1;
        Statement stncActualizarNoas = this.getConexion().createStatement();
        String sqlActualizarNoas = "UPDATE pt100 SET pt100nume = " + snoas + "  WHERE pt100tire='NS' and pt100copr='CON' and pt100cose='NOAS' and pt100nocia='" + loginU.getCompaniaActual() + "'";
        stncActualizarNoas.executeUpdate(sqlActualizarNoas);
        this.setSnoas();
    }

    public String getSnoas() {
        return "" + snoas;
    }

    public boolean setRequisicion() throws SQLException {
        ResultSet rsBuscarPtNume = null;
        Statement stncBuscarPtNume = this.getConexion().createStatement();
        String sqlBuscarPtNume = "SELECT pt100nume FROM pt100 WHERE pt100tire='ND' and pt100copr='INV' and pt100cose='REQ' and pt100nocia='" + loginU.getCompaniaActual() + "'";
        rsBuscarPtNume = stncBuscarPtNume.executeQuery(sqlBuscarPtNume);
        if (rsBuscarPtNume.next()) {
            requisicion = rsBuscarPtNume.getInt(1);
            return true;
        } else {
            return false;
        }
    }

    public void actualizaRequisicion() throws SQLException {
        requisicion += 1;
        Statement stncActualizarNoas = this.getConexion().createStatement();
        String sqlActualizarNoas = "UPDATE pt100 SET pt100nume = " + requisicion + "  WHERE pt100tire='ND' and pt100copr='INV' and pt100cose='REQ' and pt100nocia='" + loginU.getCompaniaActual() + "'";
        stncActualizarNoas.executeUpdate(sqlActualizarNoas);
        this.setSnoas();
    }

    public String getRequisicion() {
        return "" + requisicion;
    }

    public String secuencialInternoCheque() throws SQLException {
        String sqlSecuencialCheque = "select pt100nume from pt100 where pt100tire = 'NS' and pt100copr = 'BCQ' and pt100cose = 'NOSE' and pt100nocia = '" + compania + "'";
        Statement stncSecuencialCheque = this.getConexion().createStatement();
        ResultSet rsSecuencialCheque = stncSecuencialCheque.executeQuery(sqlSecuencialCheque);
        rsSecuencialCheque.next();
        String noSecuencial = String.valueOf(Integer.parseInt(rsSecuencialCheque.getString("pt100nume")) + 1);
        rsSecuencialCheque.close();
        stncSecuencialCheque.close();
        String sqlUpSecuencialCheque = "update pt100 set pt100nume = '" + noSecuencial + "' where pt100nocia = '" + compania + "' and pt100tire = 'NS' and pt100copr = 'BCQ' and pt100cose = 'NOSE'";
        Statement stncUpSecuencialCheque = this.getConexion().createStatement();
        stncUpSecuencialCheque.executeUpdate(sqlUpSecuencialCheque);
        stncUpSecuencialCheque.close();
        return noSecuencial;
    }

    public String solicitudCheque() throws SQLException {
        String sqlSolicitudCheque = "select pt100nume from pt100 where pt100tire = 'ND' and pt100copr = 'BCQ' and pt100cose = 'SOL' and pt100nocia = '" + compania + "'";
        Statement stncSolicitudCheque = this.getConexion().createStatement();
        ResultSet rsSolicitudCheque = stncSolicitudCheque.executeQuery(sqlSolicitudCheque);
        rsSolicitudCheque.next();
        String noSolicitud = String.valueOf(Integer.parseInt(rsSolicitudCheque.getString("pt100nume")) + 1);
        rsSolicitudCheque.close();
        stncSolicitudCheque.close();
        String sqlUpSolicitudCheque = "update pt100 set pt100nume = '" + noSolicitud + "' where pt100nocia = '" + compania + "' and pt100tire = 'ND' and pt100copr = 'BCQ' and pt100cose = 'SOL'";
        Statement stncUpSolicitudCheque = this.getConexion().createStatement();
        stncUpSolicitudCheque.executeUpdate(sqlUpSolicitudCheque);
        stncUpSolicitudCheque.close();
        return noSolicitud;
    }

    public String numeroReferenciaCxp() throws SQLException {
        String sqlNumeroReferencia = "select pt100nume from pt100 where pt100tire = 'NS' and pt100copr = 'CXP' and pt100cose = 'NOR' and pt100nocia = '" + compania + "'";
        Statement stncNumeroReferencia = this.getConexion().createStatement();
        ResultSet rsNumeroReferencia = stncNumeroReferencia.executeQuery(sqlNumeroReferencia);
        rsNumeroReferencia.next();
        String numeroReferencia = String.valueOf(Integer.parseInt(rsNumeroReferencia.getString("pt100nume")) + 1);
        rsNumeroReferencia.close();
        stncNumeroReferencia.close();
        String sqlUpNumeroReferencia = "update pt100 set pt100nume = '" + numeroReferencia + "' where pt100nocia = '" + compania + "' and pt100tire = 'NS' and pt100copr = 'CXP' and pt100cose = 'NOR'";
        Statement stncUpNumeroReferencia = this.getConexion().createStatement();
        stncUpNumeroReferencia.executeUpdate(sqlUpNumeroReferencia);
        stncUpNumeroReferencia.close();
        return numeroReferencia;
    }

    public String numeroEgresoInventario() throws SQLException {
        String sqlNumeroEgreso = "select pt100nume from pt100 where pt100tire='ND' and pt100copr='INV' and pt100cose='EGR' and pt100nocia = '" + compania + "'";
        Statement stncNumeroEgreso = this.getConexion().createStatement();
        ResultSet rsNumeroEgreso = stncNumeroEgreso.executeQuery(sqlNumeroEgreso);
        rsNumeroEgreso.next();
        String numeroEgreso = String.valueOf(Integer.parseInt(rsNumeroEgreso.getString("pt100nume")) + 1);
        rsNumeroEgreso.close();
        stncNumeroEgreso.close();
        String sqlUpNumeroEgreso = "update pt100 set pt100nume = " + numeroEgreso + " where pt100tire='ND' and pt100copr='INV' and pt100cose='EGR' and pt100nocia = '" + compania + "'";
        Statement stncUpNumeroEgreso = this.getConexion().createStatement();
        stncUpNumeroEgreso.executeUpdate(sqlUpNumeroEgreso);
        stncUpNumeroEgreso.close();
        return numeroEgreso;
    }       
}